package com.zijie.hosptal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.aop.AutoFill;
import com.zijie.hosptal.aop.OperationType;
import com.zijie.hosptal.mapper.AdminMapper;
import com.zijie.hosptal.pojo.dto.Account;
import com.zijie.hosptal.pojo.po.Admin;
import com.zijie.hosptal.service.AdminService;
import com.zijie.hosptal.util.Md5Util;
import com.zijie.hosptal.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    private AdminMapper adminMapper;

    @Autowired
    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Override
    public Page<Admin> list(Page<Admin> page, Admin admin) {
        return adminMapper.list(page, admin);
    }


    @AutoFill(OperationType.INSERT)
    @Override
    public boolean save(Admin admin) {
        //密码加密
        String md5String = Md5Util.getMD5String(admin.getPassword());
        //存储新的密码
        admin.setPassword(md5String);
        return adminMapper.insert(admin) > 0;
    }

    @Override
    public String removeById(Integer id) {
        //查看要删除的管理员的用户名
        String username = adminMapper.getById(id).getUsername();
        adminMapper.deleteById(id);
        return username;
    }

    @AutoFill(OperationType.UPDATE)
    @Override
    public boolean updateById(Admin admin) {
        //密码加密
        String md5String = Md5Util.getMD5String(admin.getPassword());
        admin.setPassword(md5String);
        return adminMapper.updateById(admin) > 0;
    }

    @Override
    public Admin getById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public Admin getByUsername(Account account) {
        return adminMapper.getByUsername(account);
    }
}
